package cj.web.yc.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cj.web.admin.constants.Constants;
import cj.web.admin.utils.ReturnUUID;
import cj.web.yc.constants.MemberTypeEnum;
import cj.web.yc.constants.PayHistoryTypeEnum;
import cj.web.yc.constants.PayStatusEnum;
import cj.web.yc.constants.PayWayEnum;
import cj.web.yc.dao.MemberDAO;
import cj.web.yc.dao.PayHistoryDAO;
import cj.web.yc.dao.VideoOrderDAO;
import cj.web.yc.domain.Member;
import cj.web.yc.domain.PayHistory;

@Service
@Transactional
public class MemberService {

	@Autowired
    private MemberDAO memberDAO;
	
	@Autowired
    private PayHistoryDAO payHistoryDAO;
	
	@Autowired
    private VideoOrderDAO videoOrderDAO;
	
	/**
	 * 查询
	 * @param member
	 * @return objLis;size
	 */
	public Map query(Member member){
		Map rep = new HashMap();
		List<Member> objLis = memberDAO.queryByPhone(member);
		for(Member obj:objLis){
			if(MemberTypeEnum.XS.getValue()==obj.getType()){
				obj.setTypeName(MemberTypeEnum.XS.getName());
			}else{
				obj.setTypeName(MemberTypeEnum.LS.getName());
			}
		}
		int size = memberDAO.queryByPhoneSize(member); 
		rep.put("objLis", objLis);
		rep.put("size", size);
		return rep;
    }
	
	public Member queryByPhone(String phone){
		Member obj = memberDAO.queryByPhone(phone);
		return obj;
    }
	
	public Member queryById(int id){
		Member obj = memberDAO.queryById(id);
		return obj;
    }
	
	public void create(Member member){
		memberDAO.create(member);
	}
	
	public void update(Member member){
		memberDAO.update(member);
	}
	
	public void deleteById(int id){
		memberDAO.deleteById(id);
	}
	
	public synchronized JSONObject recharge(int id,int point,String updateUser){
		
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			//新增订单
			String orderNo = ReturnUUID.getOrderNo("YC");
			int status = PayStatusEnum.YFK.getValue();
			videoOrderDAO.create(0, orderNo, id, "0.0", status, updateUser, updateUser, PayWayEnum.HTCJF.getValue());
			
			//更新积分
			memberDAO.updatePointById(id, point, updateUser);
			
			//新增消费记录
			PayHistory payHistory = new PayHistory();
			payHistory.setMemberId(id);
			payHistory.setType(PayHistoryTypeEnum.HTCJF.getValue());
			payHistory.setCount(point);
			payHistory.setMemo("后台操作充值积分，操作人员："+updateUser);
			payHistoryDAO.create(payHistory);
			
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return jo;
	}
}